﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using KerriganDirection.DirectionServiceWCF;

namespace KerriganDirection.Helpers
{
    public class ServiceProxy
    {
        public delegate void FaultedServiceEventHandler();
        public static event FaultedServiceEventHandler ServiceFaulted;
        public static ChannelFactory<IDirectionService> myServiceFactory;


        private static IDirectionService _Service;
        public static IDirectionService Service
        {
            get
            {
                if (myServiceFactory == null)
                    InitService();

                return ServiceProxy._Service;
            }
            set { ServiceProxy._Service = value; }
        }

        /// <summary>
        /// Service Init
        /// </summary>
        public static void InitService()
        {


            myServiceFactory = new ChannelFactory<IDirectionService>("KerriganService.ServiceClient.DirectionService");

            _Service = myServiceFactory.CreateChannel();
            ((ICommunicationObject)_Service).Faulted += new EventHandler(ServiceProxy_Faulted);
            ((IContextChannel)_Service).OperationTimeout = new TimeSpan(0, 1, 30);
        }

        static void ServiceProxy_Faulted(object sender, EventArgs e)
        {
            if (ServiceFaulted != null)
                ServiceFaulted();
        }
    }
}
